package stuff;

import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class FixedRowDocListener implements DocumentListener {
	
	private JTextArea ta;
	private int maxLine;
	public FixedRowDocListener(JTextArea ta, int maxLine) {
		this.ta = ta;
		this.maxLine = maxLine;
	}
	
	@Override
	public void changedUpdate(DocumentEvent e) {
		updateLineCount();
	}

	@Override
	public void insertUpdate(DocumentEvent e) {
		updateLineCount();
	}

	@Override
	public void removeUpdate(DocumentEvent e) {
		updateLineCount();
	}
	
	private void updateLineCount() {
        int lineCount = ta.getLineCount();
        if (lineCount <= this.maxLine) {
            ta.setRows(lineCount);
            ta.revalidate();
        }
    }
}
